﻿using Flurl.Http;
using GlobalVariableClass;
using Newtonsoft.Json;
using Serilog;
using System.Numerics;
using System.Reflection.Emit;

namespace Hairou_robot
{
    public static class AgvPost
    {
        public static int TaskPriority { get; set; } = 1;
        public static int groupPriority { get; set; } = 360000;
        //出库任务
        public static async Task<bool> CarryTask(string TaskOrderCode,string BoxCode,string toLocationCode)
        {
            try
            {
                EssRequestBody essRequestBody = new EssRequestBody
                {
                    taskType = "carry",
                    taskGroupCode = $"taskGroupCode-{TaskPriority}",
                    groupPriority = groupPriority,
                    tasks = new List<Tasks>
                    {
                        new Tasks
                        {
                            taskCode = TaskOrderCode,
                            taskPriority = TaskPriority,
                            taskDescribe = new TaskDescribe
                            {
                                containerCode = BoxCode,
                                containerType = "CT_KUBOT_STANDARD",
                                storageTag = "",
                                fromLocationCode = "",
                                toStationCode = "haiport_station_1",
                                toLocationCode = toLocationCode,
                                isPutaway = false
                            }
                        }
                    }
                };
                bool result = await SendTaskRequest(essRequestBody);
                if (result)
                {
                    groupPriority--;
                }
                return result;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //入库任务
        public static async Task<bool> PutWayTask(string TaskOrderCode, string BoxCode, string toLocationCode,string fromLocationCode)
        {
            try
            {
                EssRequestBody essRequestBody = new EssRequestBody
                {
                    taskType = "putaway",
                    taskGroupCode = $"taskGroupCode-{TaskPriority}",
                    groupPriority = groupPriority,
                    tasks = new List<Tasks>
                    {
                        new Tasks
                        {
                            taskCode = TaskOrderCode,
                            taskPriority = TaskPriority,
                            taskDescribe = new TaskDescribe
                            {
                                containerCode = BoxCode,
                                containerType = "CT_KUBOT_STANDARD",
                                storageTag = "",
                                fromLocationCode = fromLocationCode,
                                toStationCode = "haiport_station_1",
                                toLocationCode = toLocationCode,
                                isPutaway = false
                            }
                        }
                    }
                };
                bool result = await SendTaskRequest(essRequestBody);
                return result;
            }
            catch (Exception ex)
            {

                //在这里我想使用Serilog输出日Serilog志到C:\Log_ess   Serilog相关nuget我也没有安装
                return false;
            }
        }

        public static void test()
        {


            LogHelper.Init(); // 确保已初始化
            Log.Error("PutWayTask发生异常，TaskOrderCode: " +
                    "{TaskOrderCode}, BoxCode: " +
                    "{BoxCode}, toLocationCode:" +
                    " {toLocationCode}, fromLocationCode: {fromLocationCode}");


            
        }
        public static async Task<bool> SendTaskRequest(EssRequestBody essRequestBody)
        {


        

     
            try
            {
                var url = $"http://{GlobalVariableParameter.EssIp_Par}/task/create";
                var data = essRequestBody;
                string result = await url.WithTimeout(3000).PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                string res = Response.data.ToString();
                if (Response.msg == "success")
                {
                   
                    return true;
                }
                else
                {
                    LogHelper.Init(); // 确保已初始化
                    string essRequestBodyJson = JsonConvert.SerializeObject(essRequestBody, Formatting.Indented);
                    Log.Error($"SendTaskRequest异常，body:{essRequestBodyJson}");
                    return false;
                }

            }
            catch (Exception ex)
            {
                LogHelper.Init(); // 确保已初始化
                string essRequestBodyJson = JsonConvert.SerializeObject(essRequestBody, Formatting.Indented);
                Log.Error($"SendTaskRequest异常，body:{essRequestBodyJson}+{ex}");
                return false;
            }
        }
    }
}
